
<%@ WebHandler Language="C#" Class="viewServer" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;

public class viewServer : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string action = context.Request["action"].ToString();       
        if (action == "countPhoto") //统计共有多少图片
        {
            string sql = "select count(*) from T_SmallPhotos";
            DataTable dt = sqlHelper.GetTable(sql);
            int count = Convert.ToInt32(dt.Rows[0][0]);
            context.Response.Write(count.ToString());

        }
        else if (action == "getData")  //请求图片数据
        {
            string startId = context.Request["startId"].ToString();
            string endId = context.Request["endId"].ToString();
            //string sqlStr = string sqlStr = "SELECT   * FROM  (SELECT   id, [imageName], [imageUrl], [imageAlt], [notes], Row_Number() OVER (ORDER BY id) rownum FROM  T_SmallPhotos) t  WHERE   t .rownum >= 1 AND t .rownum <=5"
            
            //这个查询语句有点小复杂，使用了开窗函数
            string sqlStr = "SELECT   * FROM  (SELECT   id, [imageName], [imageUrl], [imageAlt], [notes], Row_Number() OVER (ORDER BY id) rownum FROM  T_SmallPhotos) t  WHERE   t .rownum >= @startId AND t .rownum <= @endId";
            //string sqlStr = "SELECT [id], [imageName], [imageUrl], [imageAlt], [notes] FROM [T_SmallPhotos] where id>1 and id<10";
            SqlParameter[] param = new SqlParameter[] {new SqlParameter("@startId",startId),
                                                    new SqlParameter("@endId",endId)};
            DataTable dt = sqlHelper.GetTable(sqlStr, param);
            List<Photo> list = new List<Photo>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                list.Add(new Photo(Convert.ToInt32(dt.Rows[i][0]), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString(), Convert.ToInt32(dt.Rows[i][5])));
            }
            System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();//将数据序列化为json数据
            context.Response.Write(jss.Serialize(list));
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //封装一个照片类，然后使用json传递
    public class Photo
    {
        public Photo(int i, string name, string url, string alt, string notes, int rownum)
        {
            id = i;
            imageName = name;
            imageUrl = url;
            imageAlt = alt;
            this.notes = notes;
            this.rownum = rownum;
        }
        private int id; //图片编号

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string imageName;//图片名称

        public string ImageName
        {
            get { return imageName; }
            set { imageName = value; }
        }
        private string imageUrl; //图片路径

        public string ImageUrl
        {
            get { return imageUrl; }
            set { imageUrl = value; }
        }
        private string imageAlt;    //图片描述

        public string ImageAlt
        {
            get { return imageAlt; }
            set { imageAlt = value; }
        }
        private string notes;

        public string Notes
        {
            get { return notes; }
            set { notes = value; }
        }
        private int rownum;

        public int Rownum
        {
            get { return rownum; }
            set { rownum = value; }
        }
    }
}